package io.github.zerobyteword.springfasterstartup;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;


public class AsyncInitListener implements ApplicationListener<ContextRefreshedEvent> {
    private final AsyncInitManager asyncInitManager;
    private final ApplicationContext applicationContext;


    public AsyncInitListener(AsyncInitManager asyncInitManager, ApplicationContext applicationContext) {
        this.asyncInitManager = asyncInitManager;
        this.applicationContext = applicationContext;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (this.applicationContext.equals(event.getApplicationContext())) {
            // 等待所有feature初始化完毕
            asyncInitManager.waitAllTaskFuture();
            asyncInitManager.printTimeCost();
            asyncInitManager.clearAllResource();
        }
    }
}
